package com.lanlian.cayman.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.GridView;
import android.widget.TextView;

import com.lanlian.cayman.activity.RiskStateActivity;
import com.lanlian.commonlib.base.BaseFragment;
import com.lanlian.cayman.R;
import com.lanlian.uikit.MyGridView;
import com.lanlian.uikit.ResearchSelectAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * <pre>
 *     author : huxiaobo
 *     e-mail : huxiaobo@xlanlian.com
 *     date   : 2018/11/13 15:49
 *     desc   : todo
 *     version: 1.0
 * </pre>
 */
@SuppressLint("ValidFragment")
public class RiskResearchTwoFragment extends BaseFragment implements View.OnClickListener {

    private GridView mGridView;
    private MyGridView mGridView2;
    private MyGridView mGridView3;
    private MyGridView mGridView4;
    private MyGridView mGridView5;



    private TextView mTvNext;
    private TextView mTvPrevious;

    private OnNextClickListener mOnNextClickListener;
    /**
     * 问题
     */
    ArrayList<String> quest=new ArrayList<>();

    public interface OnNextClickListener {
        void onNextClick();

        void onPreviousClick();
    }

    public RiskResearchTwoFragment(OnNextClickListener onNextClickListener) {
        this.mOnNextClickListener = onNextClickListener;
    }
    @Override
    protected void initData()
    {
        quest=new ArrayList<>();
        quest.add("1、货币市场基金、存款、债券、债券基金或简单的保本产品");
        quest.add("2、股票、货币或利率挂钩的简单结构性产品");
        quest.add("3、港股、国外股票");
        quest.add("4、期货、期权、对冲基金、证券保证金交易、商品保证金交易、外币保证金交易、认股证");
        quest.add("5、投资频率（过去一年）");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_research_two, null);
        initView(view);
        initData();
        setListeners();
        setAdapter();
        return view;
    }

    @Override
    protected void initView(View view) {
        super.initView(view);
        mGridView = view.findViewById(R.id.grid_view);
        mTvNext = view.findViewById(R.id.tv_next);
        mTvPrevious = view.findViewById(R.id.tv_previous);


        mGridView2 = view.findViewById(R.id.grid_view2);
        mGridView3 = view.findViewById(R.id.grid_view3);
        mGridView4 = view.findViewById(R.id.grid_view4);
        mGridView5 = view.findViewById(R.id.grid_view5);
    }
    public ArrayList<JSONObject> getData()
    {
        ArrayList<JSONObject> list=null;
        try {
            list=new ArrayList<>();

            JSONObject jo=new JSONObject();
            jo.put("topicId","6");
            jo.put("topicAnswerId",mGridView.getCheckedItemPosition()+26+"");
            jo.put("topicTitle",quest.get(0).substring(2));
            jo.put("topicAnswerDesc",list_mudi.get(mGridView.getCheckedItemPosition()));
            jo.put("supplementText","");

            JSONObject jo2=new JSONObject();
            jo2.put("topicId","7");
            jo2.put("topicAnswerId",mGridView2.getCheckedItemPosition()+30+"");
            jo2.put("topicTitle",quest.get(1).substring(2));
            jo2.put("topicAnswerDesc",list2.get(mGridView2.getCheckedItemPosition()));
            jo2.put("supplementText","");

            JSONObject jo3=new JSONObject();
            jo3.put("topicId","8");
            jo3.put("topicAnswerId",mGridView3.getCheckedItemPosition()+34+"");
            jo3.put("topicTitle",quest.get(2).substring(2));
            jo3.put("topicAnswerDesc",list3.get(mGridView3.getCheckedItemPosition()));
            jo3.put("supplementText","");



            JSONObject jo4=new JSONObject();
            jo4.put("topicId","9");
            jo4.put("topicAnswerId",mGridView4.getCheckedItemPosition()+38+"");
            jo4.put("topicTitle",quest.get(3).substring(2));
            jo4.put("topicAnswerDesc",list3.get(mGridView4.getCheckedItemPosition()));
            jo4.put("supplementText","");


            JSONObject jo5=new JSONObject();
            jo5.put("topicId","10");
            jo5.put("topicAnswerId",mGridView5.getCheckedItemPosition()+42+"");
            jo5.put("topicTitle",quest.get(4).substring(2));
            jo5.put("topicAnswerDesc",list5.get(mGridView5.getCheckedItemPosition()));
            jo5.put("supplementText","");
            list.add(jo);
            list.add(jo2);
            list.add(jo3);
            list.add(jo4);
            list.add(jo5);
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }

        return list;
    }

    @Override
    protected void setListeners() {
        super.setListeners();
        mTvNext.setOnClickListener(this);
        mTvPrevious.setOnClickListener(this);
    }
    ArrayList<String> list_mudi;
    ArrayList<String> list2;
    ArrayList<String> list3;

    ArrayList<String> list5;
    private void setAdapter() {
        list_mudi = new ArrayList<>();
        list_mudi.add("没有经验");
        list_mudi.add("0~4年");
        list_mudi.add("5~10年");
        list_mudi.add("超过10年");

        mGridView.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView.setAdapter(new ResearchSelectAdapter(getActivity(), list_mudi));

        list2 = new ArrayList<>();
        list2.add("没有经验");
        list2.add("0~4年");
        list2.add("5~10年");
        list2.add("超过10年");

        mGridView2.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView2.setAdapter(new ResearchSelectAdapter(getActivity(), list2));

       list3 = new ArrayList<>();
        list3.add("没有经验");
        list3.add("0~4年");
        list3.add("5~10年");
        list3.add("超过10年");

        mGridView3.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView3.setAdapter(new ResearchSelectAdapter(getActivity(), list3));



        mGridView4.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView4.setAdapter(new ResearchSelectAdapter(getActivity(), list3));


        list5 = new ArrayList<>();
        list5.add("0~10次");
        list5.add("11~50次");
        list5.add("51~100次");
        list5.add("101~500次");
        list5.add("500次以上");

        mGridView5.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        mGridView5.setAdapter(new ResearchSelectAdapter(getActivity(), list5));
    }
    /**
     * 返回null 全部填写 否则返回错误提示
     * @return
     */
    private  String checkSelect()
    {
        String s1="您未回答问题:";
        if(mGridView.getCheckedItemPosition()==-1)
        {
            return  s1+quest.get(0);
        }
        else if(mGridView2.getCheckedItemPosition()==-1)
        {
            return  s1+quest.get(1);
        }

        else if(mGridView3.getCheckedItemPosition()==-1)
        {
            return  s1+quest.get(2);
        }

        else if(mGridView4.getCheckedItemPosition()==-1)
        {
            return  s1+quest.get(3);
        }
        else if(mGridView5.getCheckedItemPosition()==-1)
        {
            return  s1+quest.get(4);
        }
        return  null;
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_next:
                RiskStateActivity fa= (RiskStateActivity)getActivity();

                String msg=checkSelect();
                if(msg!=null)
                {

                    fa.showToast(msg);
                    return;
                }
                mOnNextClickListener.onNextClick();
                break;
            case R.id.tv_previous:
                mOnNextClickListener.onPreviousClick();
                break;
            default:
                break;
        }
    }

}
